<?php

class DatabaseConnection {
  public $connection;

  // open
  public function open($host, $user, $pass) {
    $this->connection = mysql_connect($host, $user, $pass);
    if(!$this->connection) {
      die('Could not connect: ' . mysql_error());
    }
  }

  // close
  public function close() {
    mysql_close($this->connection);
  }

  // select database
  public function select($database_name) {
    mysql_select_db($database_name, $this->connection);
  }

  // query
  public function query($query) {
    $result = mysql_query($query, $this->connection);
    if(!$result) {
      echo "query failed: " . mysql_error();
    }
    //echo $query;
    return $result;
  }

  public static function sanitize($data) {
    // Uses the connection to sanitize with the used DB string format

    $data = trim($data);
    if(get_magic_quotes_gpc())
    {
      $data = stripslashes($data);
    }
    $data = mysql_real_escape_string($data);
    return $data;
  }
}

?> 
